package com.cskaoyan.controller;

import com.cskaoyan.bean.goods.Goods;
import com.cskaoyan.bo.goods.GoodsCreateAndUpdateBo;
import com.cskaoyan.bo.goods.GoodsPageAndSortBo;
import com.cskaoyan.service.goods.GoodsService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.goods.GoodsCatAndBrandVo;
import com.cskaoyan.vo.goods.GoodsDetailVo;
import com.cskaoyan.vo.goods.GoodsListVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description:
 * 是goods模块的controller层，主要功能有
 * goods/list
 * goods/create
 * goods/update
 * goods/delete
 * goods/detail
 * goods/catAndBrand
 * author:houzhitao@139.com
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    //根据分页和排序参数，获取商品列表
    @RequestMapping("list")
    @RequiresPermissions("admin:goods:list")
    public BaseRespVo list(GoodsPageAndSortBo pageAndSortBo){
        GoodsListVo goodsListVo = goodsService.goodsList(pageAndSortBo);
        return BaseRespVo.ok(goodsListVo);
    }

    @RequestMapping("create")
    @RequiresPermissions("admin:goods:create")
    public BaseRespVo create(@RequestBody GoodsCreateAndUpdateBo goodsCreateBo){
        Integer integer = goodsService.goodsCreate(goodsCreateBo);
        if(integer>0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.error(401,"插入失败，请检查参数");
    }

    @RequestMapping("delete")
    @RequiresPermissions("admin:goods:delete")
    public BaseRespVo delete(@RequestBody Goods goods){
        Integer effectNum = goodsService.goodsDelete(goods);
        return BaseRespVo.ok();
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:goods:update")
    public BaseRespVo update(@RequestBody GoodsCreateAndUpdateBo goodsUpdateBo){
        Integer integer = goodsService.goodsUpdate(goodsUpdateBo);
        if(integer>0){
            return BaseRespVo.ok();
        }
        return BaseRespVo.error(401,"参数错误");
    }

    @RequestMapping("detail")
    @RequiresPermissions("admin:goods:read")
    public BaseRespVo detail(Integer id){
        GoodsDetailVo goodsDetailVo = goodsService.goodsDetail(id);
        return BaseRespVo.ok(goodsDetailVo);
    }

    @RequestMapping("catAndBrand")
    // @RequiresPermissions("admin:goods:catAndBrand")
    public BaseRespVo catAndBrand(){
        GoodsCatAndBrandVo catAndBrandVo = goodsService.queryCatAndBrand();
        System.out.println(catAndBrandVo);
        return BaseRespVo.ok(catAndBrandVo);
    }
}
